from sklearn.neighbors import KDTree
from sklearn.metrics import accuracy_score

# 假设你已经有了分类特征 X 和对应的标签 y
kdtree = KDTree(X_train)
k = 3  # 设置最近邻数量

distances, indices = kdtree.query(X_test, k=k)  # 获取最近邻居
y_pred_kd = [y_train[idx] for idx in indices]  # 以多数投票的方式预测类别

accuracy_kd = accuracy_score(y_test, y_pred_kd)  # 计算准确性
print("Accuracy (KD Tree):", accuracy_kd)